package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wxgdth")
public class WxgdTuiHuanController {
    @Autowired
    WxgdTuiHuanService wxgdTuiHuanService;
    @Autowired
    TuiHuoService tuiHuoService;
    @Autowired
    ShenHeService shenHeService;
    @Autowired
    ChRuKuService chRuKuService;
/*
    //新增维修工单
    @RequestMapping("/addWeiXiuGongDan")
    @ResponseBody
    public void addWeiXiuGongDan(@RequestBody Weixiugongdan weixiugongdan){
        System.err.println("contr:"+weixiugongdan);
       weiXiuGongDanService.addWeiXiuGongDan(weixiugongdan);
    }*/

   //查看所有退换货的维修工单
    @RequestMapping("/selectAllwxTuiHuan")
    @ResponseBody
    public Map<String,Object> selectAllwxTuiHuan(String gdHao,String time, Integer pageNumm,
                                                 Integer pageSizee,HttpSession session){
        Page<Object> objects = PageHelper.startPage(pageNumm, pageSizee);

        String time3 = null;
        if(time != null && time != "" && !time.equals("")){
            StringBuffer time1 = new StringBuffer(time.substring(0,10));
            System.err.println("tim1:"+time1);
            int ti = Integer.parseInt(time1.substring(8))+1;
            System.err.println("ti:"+ti);
            String tistr = toString().valueOf(ti);
            if(toString().valueOf(ti).length() == 1){
                tistr = 0 + toString().valueOf(ti);
            }
            StringBuffer time2 = new StringBuffer(time1.replace(8,time1.length(), tistr));
            System.err.println("tim2:"+time2);
            time3 = time2.toString();
        }

        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("维修部长")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        System.err.println("gd2_people:"+empName);
        List<Wxgdtuihuan> list = wxgdTuiHuanService.selectAllwxTuiHuan(gdHao,time3,empName);

        Map<String,Object> map = new HashMap();
        map.put("total", objects.getTotal());
        map.put("list", list);
        return map;
    }


    //查看所有审核同意并且选择维修的退货单
    @RequestMapping("/selectwxTuiHuo")
    @ResponseBody
    public List<Tuihuo> selectwxTuiHuo() {
        return tuiHuoService.selectwxTuiHuo();
    }
    //查看所有审核同意并且选择维修的换货单
    @RequestMapping("/selectwxHuanHuo")
    @ResponseBody
    public List<Huanhuo> selectwxHuanHuo(){
        return tuiHuoService.selectwxHuanHuo();
    }

    //新增
    @RequestMapping("/addwxgdTuiHuan")
    @ResponseBody
    public void addwxgdTuiHuan(@RequestBody Wxgdtuihuan wxgdtuihuan){
        wxgdTuiHuanService.addwxgdTuiHuan(wxgdtuihuan);
    }

    //根据退货id查看退货对象
    @RequestMapping("/selectWxTuiHuoByThId")
    @ResponseBody
    public Tuihuo selectWxTuiHuoByThId(Integer thId){
        return shenHeService.selectTuiHuoByThId(thId);
    }

    //根据换货id查看换货对象
    @RequestMapping("/selectWxHuanHuoByThId")
    @ResponseBody
    public Huanhuo selectWxHuanHuoByThId(Integer hhId){
        return shenHeService.selectHuanHuoByThId(hhId);
    }

    //修改维修退换货的维修完成状态
    @RequestMapping("/updatewxtuihuanWcZtai")
    @ResponseBody
    public void updatewxtuihuanWcZtai(Integer gd2Wx,Integer gd2Id,String sm,Integer smId){
        wxgdTuiHuanService.updatewxtuihuanWcZtai(gd2Wx,gd2Id,sm,smId);
    }

    //修改维修退换货的入库状态
    @RequestMapping("/updatewxtuihuanRuKuZtai")
    @ResponseBody
    public void updatewxtuihuanRuKuZtai(Integer gd2Rk,Integer gd2Id){
        wxgdTuiHuanService.updatewxtuihuanRuKuZtai(gd2Rk,gd2Id);
    }

    //维修工单(退换货)申请入库
    @RequestMapping("/wxgdTuiHuanRuKu")
    @ResponseBody
    public void wxgdTuiHuanRuKu(@RequestBody Wxgdtuihuan wxgdtuihuan, HttpSession session){
        chRuKuService.wxgdTuiHuanRuKu(wxgdtuihuan,session);
    }
}
